package com.woniuxy.project115.kaer.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.project115.kaer.mall.entity.Evaluation;
import com.woniuxy.project115.kaer.mall.entity.OrderLog;
import com.woniuxy.project115.kaer.mall.entity.ProductOrderInfo;
import com.woniuxy.project115.kaer.mall.mapper.EvaluationMapper;
import com.woniuxy.project115.kaer.mall.mapper.OrderLogMapper;
import com.woniuxy.project115.kaer.mall.mapper.ProductOrderInfoMapper;
import com.woniuxy.project115.kaer.mall.param.EvaluationParam;
import com.woniuxy.project115.kaer.mall.service.EvaluationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 订单评价表 服务实现类
 * </p>
 *
 * @author lyc
 * @since 2025年02月17日
 */
@Service
@Transactional
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation> implements EvaluationService {
    @Resource
    private EvaluationMapper evaluationMapper;
    @Resource
    private ProductOrderInfoMapper productOrderInfoMapper;
    @Resource
    private OrderLogMapper orderLogMapper;
    @Override
    public void submitEvaluation(Integer userId, String nickName, EvaluationParam param) {
        //判断订单是否存在
        ProductOrderInfo productOrderInfo = productOrderInfoMapper.selectOne(new QueryWrapper<ProductOrderInfo>().eq("order_num",param.getOrderNum()).eq("user_id",userId));
        if (productOrderInfo == null) {
            throw new RuntimeException("订单不存在");
        }
        //判断订单是否已经评价
        if (!(productOrderInfo.getOrderState().equals("待评价")&&productOrderInfo.getPayState().equals("已支付"))) {
            throw new RuntimeException("订单已评价");
        }else {
            //修改订单状态
            productOrderInfo.setOrderState("已完成");
            productOrderInfoMapper.updateById(productOrderInfo);
            //添加评价
            Evaluation orderEvaluation = new Evaluation();
            orderEvaluation.setUserId(userId);
            orderEvaluation.setContent(param.getEvaluation());
            orderEvaluation.setOrderNum(param.getOrderNum());
            orderEvaluation.setUserName(nickName);//设置用户昵称
            orderEvaluation.setScore(param.getScore());
            evaluationMapper.insert(orderEvaluation);
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderNum(param.getOrderNum());
            orderLog.setUserId(userId);
            orderLog.setOperateType("用户评价");
            orderLog.setOperateTime(LocalDateTime.now());
            orderLogMapper.insert(orderLog);
        }
    }

    @Override
    public void autoEvaluate(String orderNum) {
        //修改订单状态
        ProductOrderInfo productOrderInfo = productOrderInfoMapper.selectById(orderNum);
        productOrderInfo.setOrderState("已完成");
        productOrderInfoMapper.updateById(productOrderInfo);
        //添加评价
        Evaluation orderEvaluation = new Evaluation();
        orderEvaluation.setUserId(1);
        orderEvaluation.setContent("系统默认好评");
        orderEvaluation.setOrderNum(orderNum);
        orderEvaluation.setUserName("机器人");//设置用户昵称
        orderEvaluation.setScore(5);
        evaluationMapper.insert(orderEvaluation);
        OrderLog orderLog = new OrderLog();
        orderLog.setOrderNum(orderNum);
        orderLog.setUserId(1);
        orderLog.setOperateType("系统自动评价");
        orderLog.setOperateTime(LocalDateTime.now());
        orderLogMapper.insert(orderLog);
    }
}
